<?php

class authorRefreshnullTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
      new sfCommandOption('count-null', null, sfCommandOption::PARAMETER_NONE, 'Count null url or content authors'),
      // add your own options here
    ));

    $this->namespace        = 'authors';
    $this->name             = 'refresh';
    $this->briefDescription = 'Set content and url to null where they have no value';
    $this->detailedDescription = <<<EOF
The [author:refresh|INFO] task sets url and content on null where they have blank value('').
Call it with:

  [php symfony author:refresh|INFO]


EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

    // add your code here
    mysql_connect('localhost', 'root', '123456');
    mysql_select_db('quotes');

    if($options['count-null'])
    {
      $result = mysql_query("SELECT * FROM `author` WHERE `url` IS NULL or `content` IS NULL");
      $this->logSection("Authors count >>", mysql_num_rows($result).' authors with null url or content');
      exit;
    }

    $result = mysql_query("UPDATE `author` SET `content`= null WHERE content = ''; ");
    $this->logSection("Author refresh", "Authors with no content have now null content --> ".mysql_affected_rows().' affected ');
    mysql_query("UPDATE `author` SET `url`= null WHERE url = ''");
    $this->logSection("Author refresh", "Authors with no url have now null url --> ".mysql_affected_rows().' affected ');
  }
}
